import 'package:flutter/material.dart';
import 'package:flutter_application_pd/pages/login/controller.dart';
import 'package:get/get.dart';

class AuthMiddleWare extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    // 检测token
    if (route!.startsWith('/login')) {
      // 检测是否已经登录
      if (Get.find<LoginController>().isLoggedIn()) {
        return const RouteSettings(name: '/');
      }
    } else if (route.startsWith('/')) {
      // 检测是否已经登录
      if (!Get.find<LoginController>().isLoggedIn()) {
        return const RouteSettings(name: '/login');
      }
    }
    // 如果不是登录页面，也不是首页，则不拦截
    return null;
  }
}
